import axios from "axios";
import { put, takeEvery } from "redux-saga/effects";
import {
  getNewSongsFailed,
  getNewSongsSuccess,
  GET_NEWSONGS,
  IGetNewSongsAction,
} from "../actions/newsongs.action";

function* handleGetNewSongs(action: IGetNewSongsAction) {
  try {
    const { data } = yield axios.get(
      "http://localhost:3000/api/personalized/newsong"
    );
    console.log("new songs--->", data);
    yield put(getNewSongsSuccess(data));
  } catch (error) {
    yield put(getNewSongsFailed(JSON.stringify(error)));
  }
}

export default function* newSongsSaga() {
  yield takeEvery(GET_NEWSONGS, handleGetNewSongs);
}
